package com.skyworth.web.common.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by skyworth on 2016/9/26.
 */
@Service
public class ScheduleTaskService {
	Logger LOG = LoggerFactory.getLogger(ScheduleTaskService.class);

	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
	private int[] delays = new int[]{8,3,6,2,2};
	private int index = 0;


	/*@Scheduled(fixedRate = 5*1000)
	public void reportCurrentTime(){
		LOG.info("每隔5秒执行一次任务 "+ dateFormat.format(new Date()));
		process();
	}*/

//	@Scheduled(cron = "0/5 * * * * ?}")
	@Scheduled(cron = "12 45 16 ? * *")
	public void fixTimeExecution(){
		LOG.info("在指定时间(16点45分12秒) "+ dateFormat.format(new  Date())+" 执行");
		process();
	}

	/*@Scheduled(fixedDelay = 5000)
	public void fixDelayExec(){
		LOG.info("延时五秒执行任务 "+ dateFormat.format(new Date()));
		process();
	}*/

	public void process() {
		try {
			if(index > delays.length - 1){
				if(index == delays.length){
					LOG.info("---------- test end at " + dateFormat.format(new Date()) + " ---------");
				}
				index ++;
				return;
			}else{
				LOG.info(index + ":start run at" + dateFormat.format(new Date()));
			}
			Thread.sleep(delays[index] * 1000);
			LOG.info(index + ":end run at " + dateFormat.format(new Date()));
			index ++;
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
